/*
========================================================================
 Name        : ExportContactsListBoxView.cpp
 Author      : etouch.cc
 Copyright   : etouch.cc,All Right Reserved!
 Description : 
========================================================================
*/
 
#include <aknviewappui.h>
#include <eikmenub.h>
#include <avkon.hrh>
#include <barsread.h>
#include <stringloader.h>
#include <aknlists.h>
#include <eikenv.h>
#include <akniconarray.h>
#include <eikclbd.h>
#include <akncontext.h>
#include <akntitle.h>
#include <eikbtgpc.h>
#include <ExportContacts.rsg>
 
#include "ExportContacts.hrh"
#include "ExportContactsListBoxView.h"
#include "ExportContactsListBox.hrh"
#include "ExportContactsSelectDirContainer.hrh"
#include "ExportContactsListBox.h"
#include "ContactEngine.h" 
#include "ExportContactsAppUi.h"

_LIT(KExportFile,"C:\\CE.txt");


CExportContactsListBoxView::CExportContactsListBoxView()
{ 
	iExportContactsListBox = NULL;
	iContactEngine = NULL;
}

 
CExportContactsListBoxView::~CExportContactsListBoxView()
{	 
	if(iExportContactsListBox)
	{
		delete iExportContactsListBox;
		iExportContactsListBox = NULL;
	}	 
}

 
CExportContactsListBoxView* CExportContactsListBoxView::NewL()
{
	CExportContactsListBoxView* self = CExportContactsListBoxView::NewLC();
	CleanupStack::Pop( self );
	return self;
}

 
CExportContactsListBoxView* CExportContactsListBoxView::NewLC()
{
	CExportContactsListBoxView* self = new ( ELeave ) CExportContactsListBoxView();
	CleanupStack::PushL( self );
	self->ConstructL();
	return self;
}
 
void CExportContactsListBoxView::ConstructL()
{ 
	BaseConstructL( R_EXPORT_CONTACTS_LIST_BOX_EXPORT_CONTACTS_LIST_BOX_VIEW);
}

 
TUid CExportContactsListBoxView::Id() const
{
	return TUid::Uid( EExportContactsListBoxViewId );
}

 
void CExportContactsListBoxView::HandleCommandL( TInt aCommand )
{ 
	TBool commandHandled = EFalse;
	CExportContactsAppUi* appUI = (CExportContactsAppUi*)AppUi();
	switch ( aCommand )
	{	 
		case EExportContactsListBoxViewExportAllContacts_MenuItemCommand:
		{			
			appUI->ActiveView(EExportContactsSelectDirContainerViewId);
		}
		break;
		case EExportContactsListBoxViewDelete_MenuItemCommand:
		{
			if(iExportContactsListBox)
			{
				iExportContactsListBox->DeleteSelectedContact();
			}
		}
		break;
		case EExportContactsListBoxViewHelp_MenuItemCommand:
		{			
			AppUi()->HandleCommandL(aCommand);
		}
		break;
		case EExportContactsListBoxViewAbout_MenuItemCommand:
		{			 
			AppUi()->HandleCommandL(aCommand);
		}
		break;
		case EAknSoftkeyExit:
		{			
			AppUi()->HandleCommandL( EEikCmdExit );
		}
		default:
		break; 
	}
}

 
void CExportContactsListBoxView::DoActivateL( 
											 const TVwsViewId& /*aPrevViewId*/,
											 TUid /*aCustomMessageId*/,
											 const TDesC8& /*aCustomMessage*/ 
											 )
{	 
	SetupStatusPaneL();			
	if ( iExportContactsListBox == NULL )
	{
		iExportContactsListBox = CreateContainerL();
		iExportContactsListBox->SetMopParent( this );
		iExportContactsListBox->SetContactEngine(iContactEngine);
		iExportContactsListBox->FillListBoxWithContacts();
		AppUi()->AddToStackL( *this, iExportContactsListBox );
	} 
}

 
void CExportContactsListBoxView::DoDeactivate()
{
	CleanupStatusPane();
	
	if ( iExportContactsListBox != NULL )
	{
		AppUi()->RemoveFromViewStack( *this, iExportContactsListBox );
		delete iExportContactsListBox;
		iExportContactsListBox = NULL;
	}	
}

void CExportContactsListBoxView::SetContactEngine(ContactEngine* aContactEngine)
{
	iContactEngine = aContactEngine;
}
 
void CExportContactsListBoxView::HandleStatusPaneSizeChange()
{
	CAknView::HandleStatusPaneSizeChange();	
	 
	TVwsViewId view;
	AppUi()->GetActiveViewId( view );
	if ( view.iViewUid == Id() )
	{
		TInt result;
		TRAP( result, SetupStatusPaneL() );
	}
}

void CExportContactsListBoxView::DynInitMenuPaneL( TInt aResourceId, CEikMenuPane* aMenuPane )
{
	if(aResourceId == R_EXPORT_CONTACTS_LIST_BOX_MENU_PANE1_MENU_PANE)
	{
		if(iExportContactsListBox)
		{
			TInt pListBoxItemCount = iExportContactsListBox->ListBoxItemCount();
			if(pListBoxItemCount == 0)
			{
				aMenuPane->SetItemDimmed(EExportContactsListBoxViewExportAllContacts_MenuItemCommand,ETrue);				
			}
			aMenuPane->SetItemDimmed(EExportContactsListBoxViewDelete_MenuItemCommand,ETrue);
		}
	}
}

void CExportContactsListBoxView::SetupStatusPaneL()
{	 
	TUid titlePaneUid = TUid::Uid( EEikStatusPaneUidTitle );
	CEikStatusPaneBase::TPaneCapabilities subPaneTitle = 
		StatusPane()->PaneCapabilities( titlePaneUid );
	if ( subPaneTitle.IsPresent() && subPaneTitle.IsAppOwned() )
	{
		CAknTitlePane* title = static_cast< CAknTitlePane* >( 
			StatusPane()->ControlL( titlePaneUid ) );
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_EXPORT_CONTACTS_LIST_BOX_TITLE_RESOURCE_2 );
		title->SetFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
	} 
	TUid contextPaneUid = TUid::Uid( EEikStatusPaneUidContext );
	CEikStatusPaneBase::TPaneCapabilities subPaneContext = 
			StatusPane()->PaneCapabilities( contextPaneUid );
	if ( subPaneContext.IsPresent() && subPaneContext.IsAppOwned() )
	{
		CAknContextPane* context = static_cast< CAknContextPane* > ( 
			StatusPane()->ControlL( contextPaneUid ) );
		TResourceReader reader;
		iCoeEnv->CreateResourceReaderLC( reader, R_EXPORT_CONTACTS_LIST_BOX_CONTEXT_RESOURCE );
		context->SetFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
	}
				
}
 
void CExportContactsListBoxView::CleanupStatusPane()
{
}
 
CExportContactsListBox* CExportContactsListBoxView::CreateContainerL()
{
	return CExportContactsListBox::NewL( ClientRect(), NULL, this );
}

